home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / tif_machdep.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  187 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_machdep.c,v 1.4 92/10/21 13:42:14 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1992 Sam Leffler
  7.  * Copyright (c) 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library Machine Dependent Routines.
  31.  */
  32. #include "tiffiop.h"
  33.  
  34. #ifdef tahoe
  35. typedef    struct ieeedouble {
  36.     u_long    sign    : 1,
  37.         exp    : 11,
  38.         mant    : 20;
  39.     u_long    mant2;
  40. } ieeedouble;
  41. typedef    struct ieeefloat {
  42.     u_long    sign    : 1,
  43.         exp    : 8,
  44.         mant    : 23;
  45. } ieeefloat;
  46.  
  47. typedef    struct {
  48.     u_long    sign    : 1,
  49.         exp    : 8,
  50.         mant    : 23;
  51.     u_long    mant2;
  52. } nativedouble;
  53. typedef    struct {
  54.     u_long    sign    : 1,
  55.         exp    : 8,
  56.         mant    : 23;
  57. } nativefloat;
  58. /*
  59.  * Beware, over/under-flow in conversions will
  60.  * result in garbage values -- handling it would
  61.  * require a subroutine call or lots more code.
  62.  */
  63. #define    NATIVE2IEEEFLOAT(fp) { \
  64.     if ((fp)->native.exp) \
  65.         (fp)->ieee.exp = (fp)->native.exp - 129 + 127;    /* alter bias */\
  66. }
  67. #define    IEEEFLOAT2NATIVE(fp) { \
  68.     if ((fp)->ieee.exp) \
  69.         (fp)->native.exp = (fp)->ieee.exp - 127 + 129;     /* alter bias */\
  70. }
  71. #define    IEEEDOUBLE2NATIVE(dp) { \
  72.     if ((dp)->native.exp = (dp)->ieee.exp) \
  73.     (dp)->native.exp += -1023 + 129; \
  74.     (dp)->native.mant = ((dp)->ieee.mant<<3)|((dp)->native.mant2>>29); \
  75.     (dp)->native.mant2 <<= 3; \
  76. }
  77. #endif /* tahoe */
  78.  
  79. #ifdef vax
  80. typedef    struct ieeedouble {
  81.     u_long    mant    : 20,
  82.         exp    : 11,
  83.         sign    : 1;
  84.     u_long    mant2;
  85. } ieeedouble;
  86. typedef    struct ieeefloat {
  87.     u_long    mant    : 23,
  88.         exp    : 8,
  89.         sign    : 1;
  90. } ieeefloat;
  91.  
  92. typedef    struct {
  93.     u_long    mant1    : 7,
  94.         exp    : 8,
  95.         sign    : 1,
  96.         mant2    : 16;
  97.     u_long    mant3;
  98. } nativedouble;
  99. typedef    struct {
  100.     u_long    mant1    : 7,
  101.         exp    : 8,
  102.         sign    : 1,
  103.         mant2    : 16;
  104. } nativefloat;
  105. /*
  106.  * Beware, these do not handle over/under-flow
  107.  * during conversion from ieee to native format.
  108.  */
  109. #define    NATIVE2IEEEFLOAT(fp) { \
  110.     float_t t; \
  111.     if (t.ieee.exp = (fp)->native.exp) \
  112.     t.ieee.exp += -129 + 127; \
  113.     t.ieee.sign = (fp)->native.sign; \
  114.     t.ieee.mant = ((fp)->native.mant1<<16)|(fp)->native.mant2; \
  115.     *(fp) = t; \
  116. }
  117. #define    IEEEFLOAT2NATIVE(fp) { \
  118.     float_t t; int v = (fp)->ieee.exp; \
  119.     if (v) v += -127 + 129;        /* alter bias of exponent */\
  120.     t.native.exp = v;            /* implicit truncation of exponent */\
  121.     t.native.sign = (fp)->ieee.sign; \
  122.     v = (fp)->ieee.mant; \
  123.     t.native.mant1 = v >> 16; \
  124.     t.native.mant2 = v;\
  125.     *(fp) = t; \
  126. }
  127. #define    IEEEDOUBLE2NATIVE(dp) { \
  128.     double_t t; int v = (dp)->ieee.exp; \
  129.     if (v) v += -1023 + 129;         /* if can alter bias of exponent */\
  130.     t.native.exp = v;            /* implicit truncation of exponent */\
  131.     v = (dp)->ieee.mant; \
  132.     t.native.sign = (dp)->ieee.sign; \
  133.     t.native.mant1 = v >> 16; \
  134.     t.native.mant2 = v;\
  135.     t.native.mant3 = (dp)->mant2; \
  136.     *(dp) = t; \
  137. }
  138. #endif /* vax */
  139.  
  140. #if !HAVE_IEEEFP
  141. #if !defined(IEEEFLOAT2NATIVE) || !defined(NATIVE2IEEEFLOAT)
  142. "Help, you've configured the library to not have IEEE floating point,\
  143. but not defined how to convert between IEEE and native formats!"
  144. #endif
  145.  
  146. /*
  147.  * These unions are used during floating point
  148.  * conversions.  The above macros define the
  149.  * conversion operations.
  150.  */
  151. typedef    union {
  152.     ieeedouble    ieee;
  153.     nativedouble    native;
  154.     char        b[8];
  155.     double        d;
  156. } double_t;
  157.  
  158. typedef    union {
  159.     ieeefloat    ieee;
  160.     nativefloat    native;
  161.     char        b[4];
  162.     float        f;
  163. } float_t;
  164.  
  165. void
  166. DECLARE3(TIFFCvtIEEEFloatToNative, TIFF*, tif, u_int, n, float*, f)
  167. {
  168.     float_t *fp = (float_t *)f;
  169.  
  170.     while (n-- > 0) {
  171.         IEEEFLOAT2NATIVE(fp);
  172.         fp++;
  173.     }
  174. }
  175.  
  176. void
  177. DECLARE3(TIFFCvtNativeToIEEEFloat, TIFF*, tif, u_int, n, float*, f)
  178. {
  179.     float_t *fp = (float_t *)f;
  180.  
  181.     while (n-- > 0) {
  182.         NATIVE2IEEEFLOAT(fp);
  183.         fp++;
  184.     }
  185. }
  186. #endif
  187.